// 节流
export function throttle(fn: { apply: (arg0: any, arg1: any[]) => void }, t: number) {
  let flag = true
  const interval = t || 500
  return function (this: any, ...args: any) {
    if (flag) {
      fn.apply(this, args)
      flag = false
      setTimeout(() => {
        flag = true
      }, interval)
    }
  }
}

// 防抖
export function debounce(fn: { apply: (arg0: any, arg1: any) => void }, t: number = 500) {
  let timeId: any = null
  const delay = t;
  return function (this: any, ...args: any) {
    if (timeId) {
      clearTimeout(timeId)
    }
    timeId = setTimeout(() => {
      timeId = null
      fn.apply(this, args)
    }, delay)
  }
}

// 地图点线面颜色
export function getstyleJson() {
  return {
    /** 点 */
    'Point': {
      image: {
        radius: 5,
        fill: {
          color: 'rgba(255, 255, 0, 0.1)'
        },
        stroke: {color: 'red', width: 1}
      }
    },
    /** 线 */
    'LineString': {
      stroke: {
        color: 'green',
        width: 1
      }
    },
    /** 面 */
    'Polygon': {
      stroke: {
        color: 'red',
        lineDash: [4],
        width: 1
      },
      fill: {
        color: 'rgba(255, 255, 0, 0.5)'
      }
    },
    /** 圆 */
    'Circle': {
      stroke: {
        color: 'red',
        width: 2
      },
      fill: {
        color: 'rgba(255,0,0,0.2)'
      }
    }
  }
}

export function getStyleArray() {
  return [
    {stroke: {color: 'rgba(241,4,4,1)', lineDash: [4], width: 1}, fill: {color: 'rgba(245,97,97,.5)'}},
    {stroke: {color: 'rgba(4,149,74,1)', lineDash: [4], width: 1}, fill: {color: 'rgba(89,238,161,.5)'}},
    {stroke: {color: 'rgba(5,85,214,1)', lineDash: [4], width: 1}, fill: {color: 'rgba(71,148,230,.5)'}},
    {stroke: {color: 'rgba(252,125,6,1)', lineDash: [4], width: 1}, fill: {color: 'rgba(252,125,6,.5)'}}
  ];
}

// 地图点线面颜色
export function getTools() {
  return [
    {
      icon: '<svg class="tool-icon tool-color-0" aria-hidden="true">\n' +
        '        <use xlink:href="#iconsuoxiao"></use>\n' +
        '          </svg>', type: 0, select: false, title: '缩小'
    },
    {
      icon: '<svg class="tool-icon tool-color-1" aria-hidden="true">\n' +
        '            <use xlink:href="#iconfangda"></use>\n' +
        '          </svg>', type: 1, select: false, title: '放大'
    },
    // {icon: '<svg class="tool-icon tool-color-2" aria-hidden="true">\n' +
    //     '            <use xlink:href="#iconquanping"></use>\n' +
    //     '          </svg>', type: 2, select: false, title: '全屏'},
    {
      icon: '<svg class="tool-icon tool-color-3" aria-hidden="true">\n' +
        '            <use xlink:href="#iconliangchangdu"></use>\n' +
        '          </svg>', type: 3, select: false, title: '测量'
    },
    {
      icon: '<svg class="tool-icon tool-color-4" aria-hidden="true">\n' +
        '            <use xlink:href="#iconliangmianji"></use>\n' +
        '          </svg>', type: 4, select: false, title: '计算面积'
    },
    // {icon: '<svg class="tool-icon tool-color-5" aria-hidden="true">\n' +
    //     '            <use xlink:href="#icontuceng"></use>\n' +
    //     '          </svg>', type: 5, select: false, title: '图层'},
    {
      icon: '<svg class="tool-icon tool-color-6" aria-hidden="true">\n' +
        '            <use xlink:href="#iconshouqi"></use>\n' +
        '          </svg>', type: 6, select: false, title: '收起'
    },
  ]
}

export function timestampFormat(type: any, timestamp: any) {
  let time = new Date(timestamp);
  let y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mm = time.getMinutes();
  let s = time.getSeconds();
  if (type === 1) {
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  } else if (type === 2) {
    return y + '-' + add0(m) + '-' + add0(d) + ' ';
  } else {
    return y + '年' + add0(m) + '月' + add0(d) + '日 ';
  }

  function add0(n: any) {
    return n < 10 ? '0' + n : n;
  }
}

/* 时间yyyy-MM-dd HH:mm:ss转为时间戳 */
export function timeToTimestamp(time: any) {
  // 获取某个时间格式的时间戳
  // let stringTime = "2014-07-10 10:21:12";
  let timestamp = Date.parse(new Date(time).toString());
  return timestamp;
}

export function getSaveTimeData(time: Date, type: number = 0, timeShow = true) {
  let y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mm = time.getMinutes();
  let s = time.getSeconds();

  if (type === 1) {
    if (timeShow) {
      return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    } else {
      return y + '-' + add0(m) + '-' + add0(d);
    }
  } else {
    if (timeShow) {
      return y + '年' + add0(m) + '月' + add0(d) + '日 ' + add0(h) + ':' + add0(mm);
    } else {
      return y + '年' + add0(m) + '月' + add0(d) + '日 ';
    }
  }

  function add0(n: any) {
    return n < 10 ? '0' + n : n;
  }
}

export function copyObject(obj: any): any {
  return JSON.parse(JSON.stringify(obj));
}

export function getName(list: any[], value: any) {
  let name = '';
  list.forEach(e => {
    if (e['value'] === value) {
      name = e['label'];
      return;
    }
  });
  return name;
}

export function getCurrentTimestampFormat(time: Date) {
  let y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mm = time.getMinutes();
  let s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);

  function add0(n: number) {
    return n < 10 ? '0' + n : n;
  }
}